
$(function()
{
	goToPage(1);
});

function goToPage(pagenum)
{
	$("#contentInfo").empty();
	$("#rolepage").empty();
	$.ajax({
		url : contextPath+ 'user/role/list',
		data : {
			pageszie : 10,
			pagenum : pagenum
		},
		type : 'POST',
		success : function(list) {

			$("#contentInfo").empty();
			$("#rolepage").empty();
			
			if(list!=null && list.totalRecord>0)
			{			
				var obj = "";
				for(var i=0;i<list.records.length;i++)
				{
					var companyrole = list.records[i];
					obj += "<div class='pwsj_right_group_cont'>";
					obj += "<ul class='group_cont_ul'>";
					obj += "<li class='number'>"+(i+1)+"</li>";
					obj += "<li class='group_name'>"+companyrole.roleName+"</li>";
					obj += "<li class='manage_item' style='border-right:none;'>";
	
					if(isVip && canEdit)
					{
						obj += "<a href='#' onclick='javascrpit:openrole(\""+companyrole.roleName+"\","+companyrole.companyRoleId+");'>权限设定</a>";
					}
					if(isVip){
						obj += "<a href='membermanagement.jsp?roleid="+companyrole.companyRoleId+"'>成员管理</a>";
					}			
					
					if(isVip && companyrole.canDelete != 2&&canDelete)
					{
						obj += "<a href='#' onclick='javascrpit:delrole("+companyrole.companyRoleId+");'>删除分组</a>";
					}
					obj += "</li>";
					obj += "</ul>";
					obj += "</div>";	
				}
				$("#contentInfo").append(obj);
				pageList(list);
			}
		 
		}
		
	});
}
function selectComanyAction(comanyname,roleid)
{
	//alert(comanyname);
	$.ajax({
		url : contextPath+ 'user/role/actionlist',
		data:{
			companyRoleId : roleid
		},
		type : 'POST',
		success : function(list) {
			$(".add_group_box").empty();
			var obj = "<div class='add_group_box_title'>添加分组<img src='../images/add_members_close.jpg' alt='' class='close' onclick='javascrpit:colse();'/></div>";
			obj += "<div class='group_name_box'>分组名称：";
			if(comanyname == ""){
				obj += "<input class='group_name_text' placeholder='请输入单位名称' type='text' name='cname' id='cname'/>";
			}else{
				obj += "<input class='group_name_text' placeholder='请输入单位名称' type='text' name='cname' id='cname' value='"+ comanyname +"'/>";
			}
			obj += "</div><div class='limit_option_outbox'><div class='limit_select_text'>权限选择：</div>";
			obj += "<div class='limit_option_box'><div class='big'><div class='title'>单位管理</div><ul class='limit_option_ul'>";
			var action = list[1];
			obj += "<li class='limit_option_li'>";
			if(action.actionType==-1){
				obj += "<input class='checkbox' type='checkbox' id='ckid' name='chkItem' value='"+action.actionCode+"' checked/>"+action.actionName;
			}else{
				obj += "<input class='checkbox' type='checkbox' id='ckid' name='chkItem' value='"+action.actionCode+"'/>"+action.actionName;
			}
			obj += "</li>";
			action = list[2];
			obj += "<li class='limit_option_li'>";
			if(action.actionType==-1){
				obj += "<input class='checkbox' type='checkbox' id='ckid' name='chkItem' value='"+action.actionCode+"' checked/>"+action.actionName;
			}else{
				obj += "<input class='checkbox' type='checkbox' id='ckid' name='chkItem' value='"+action.actionCode+"'/>"+action.actionName;
			} 
			obj += "</li>";
			action = list[3];
			obj += "<li class='limit_option_li'>";
			if(action.actionType==-1){
				obj += "<input class='checkbox' type='checkbox' id='ckid' name='chkItem' value='"+action.actionCode+"' checked/>"+action.actionName;
			}else{
				obj += "<input class='checkbox' type='checkbox' id='ckid' name='chkItem' value='"+action.actionCode+"'/>"+action.actionName;
			} 
			obj += "</li>";
			action = list[4];
			obj += "<li class='limit_option_li'>";
			if(action.actionType==-1){
				obj += "<input class='checkbox' type='checkbox' id='ckid' name='chkItem' value='"+action.actionCode+"' checked/>"+action.actionName;
			}else{
				obj += "<input class='checkbox' type='checkbox' id='ckid' name='chkItem' value='"+action.actionCode+"'/>"+action.actionName;
			} 
			obj += "</li>";
			action = list[5];
			obj += "<li class='limit_option_li'>";
			if(action.actionType==-1){
				obj += "<input class='checkbox' type='checkbox' id='ckid' name='chkItem' value='"+action.actionCode+"' checked/>"+action.actionName;
			}else{
				obj += "<input class='checkbox' type='checkbox' id='ckid' name='chkItem' value='"+action.actionCode+"'/>"+action.actionName;
			}
			action = list[28];
			obj += "<li class='limit_option_li'>";
			if(action.actionType==-1){
				obj += "<input class='checkbox' type='checkbox' id='ckid' name='chkItem' value='"+action.actionCode+"' checked/>"+action.actionName;
			}else{
				obj += "<input class='checkbox' type='checkbox' id='ckid' name='chkItem' value='"+action.actionCode+"'/>"+action.actionName;
			} 
			obj += "</li></ul><div class='clear'></div></div>";
			obj += "<div class='big'><div class='title'>操作日志</div><ul class='limit_option_ul'>";
			action = list[14];
			obj += "<li class='limit_option_li'>";
			if(action.actionType==-1){
				obj += "<input class='checkbox' type='checkbox' id='ckid' name='chkItem' value='"+action.actionCode+"' checked/>"+action.actionName;
			}else{
				obj += "<input class='checkbox' type='checkbox' id='ckid' name='chkItem' value='"+action.actionCode+"'/>"+action.actionName;
			} 
			obj += "</li></ul><div class='clear'></div></div>";
			obj += "<div class='big'><div class='title'>权限管理</div><ul class='limit_option_ul'>";
			action = list[10];
			obj += "<li class='limit_option_li'>";
			if(action.actionType==-1){
				obj += "<input class='checkbox' type='checkbox' id='ckid' name='chkItem' value='"+action.actionCode+"' checked/>"+action.actionName;
			}else{
				obj += "<input class='checkbox' type='checkbox' id='ckid' name='chkItem' value='"+action.actionCode+"'/>"+action.actionName;
			}
			obj += "</li>";
			action = list[11];
			obj += "<li class='limit_option_li'>";
			if(action.actionType==-1){
				obj += "<input class='checkbox' type='checkbox' id='ckid' name='chkItem' value='"+action.actionCode+"' checked/>"+action.actionName;
			}else{
				obj += "<input class='checkbox' type='checkbox' id='ckid' name='chkItem' value='"+action.actionCode+"'/>"+action.actionName;
			} 
			obj += "</li>";
			action = list[12];
			obj += "<li class='limit_option_li'>";
			if(action.actionType==-1){
				obj += "<input class='checkbox' type='checkbox' id='ckid' name='chkItem' value='"+action.actionCode+"' checked/>"+action.actionName;
			}else{
				obj += "<input class='checkbox' type='checkbox' id='ckid' name='chkItem' value='"+action.actionCode+"'/>"+action.actionName;
			} 
			obj += "</li>";
			action = list[13];
			obj += "<li class='limit_option_li'>";
			if(action.actionType==-1){
				obj += "<input class='checkbox' type='checkbox' id='ckid' name='chkItem' value='"+action.actionCode+"' checked/>"+action.actionName;
			}else{
				obj += "<input class='checkbox' type='checkbox' id='ckid' name='chkItem' value='"+action.actionCode+"'/>"+action.actionName;
			} 
			obj += "</li></ul><div class='clear'></div></div>";
			obj += "<div class='big'><div class='title'>单位人员管理</div><ul class='limit_option_ul'>";
			action = list[6];
			obj += "<li class='limit_option_li'>";
			if(action.actionType==-1){
				obj += "<input class='checkbox' type='checkbox' id='ckid' name='chkItem' value='"+action.actionCode+"' checked/>"+action.actionName;
			}else{
				obj += "<input class='checkbox' type='checkbox' id='ckid' name='chkItem' value='"+action.actionCode+"'/>"+action.actionName;
			}
			obj += "</li>";
			action = list[7];
			obj += "<li class='limit_option_li'>";
			if(action.actionType==-1){
				obj += "<input class='checkbox' type='checkbox' id='ckid' name='chkItem' value='"+action.actionCode+"' checked/>"+action.actionName;
			}else{
				obj += "<input class='checkbox' type='checkbox' id='ckid' name='chkItem' value='"+action.actionCode+"'/>"+action.actionName;
			} 
			obj += "</li>";
			action = list[8];
			obj += "<li class='limit_option_li'>";
			if(action.actionType==-1){
				obj += "<input class='checkbox' type='checkbox' id='ckid' name='chkItem' value='"+action.actionCode+"' checked/>"+action.actionName;
			}else{
				obj += "<input class='checkbox' type='checkbox' id='ckid' name='chkItem' value='"+action.actionCode+"'/>"+action.actionName;
			} 
			obj += "</li>";
			action = list[9];
			obj += "<li class='limit_option_li'>";
			if(action.actionType==-1){
				obj += "<input class='checkbox' type='checkbox' id='ckid' name='chkItem' value='"+action.actionCode+"' checked/>"+action.actionName;
			}else{
				obj += "<input class='checkbox' type='checkbox' id='ckid' name='chkItem' value='"+action.actionCode+"'/>"+action.actionName;
			}
			obj += "</li>";
			action = list[29];
			obj += "<li class='limit_option_li'>";
			if(action.actionType==-1){
				obj += "<input class='checkbox' type='checkbox' id='ckid' name='chkItem' value='"+action.actionCode+"' checked/>"+action.actionName;
			}else{
				obj += "<input class='checkbox' type='checkbox' id='ckid' name='chkItem' value='"+action.actionCode+"'/>"+action.actionName;
			}
			obj += "</li>";
			action = list[30];
			obj += "<li class='limit_option_li'>";
			if(action.actionType==-1){
				obj += "<input class='checkbox' type='checkbox' id='ckid' name='chkItem' value='"+action.actionCode+"' checked/>"+action.actionName;
			}else{
				obj += "<input class='checkbox' type='checkbox' id='ckid' name='chkItem' value='"+action.actionCode+"'/>"+action.actionName;
			}
			obj += "</li></ul><div class='clear'></div></div>";
			obj += "<div class='big'><div class='title'>中压管理</div><ul class='limit_option_ul'>";
			action = list[15];
			obj += "<li class='limit_option_li'>";
			if(action.actionType==-1){
				obj += "<input class='checkbox' type='checkbox' id='ckid' name='chkItem' value='"+action.actionCode+"' checked/>"+action.actionName;
			}else{
				obj += "<input class='checkbox' type='checkbox' id='ckid' name='chkItem' value='"+action.actionCode+"'/>"+action.actionName;
			} 
			obj += "</li>";
			action = list[16];
			obj += "<li class='limit_option_li'>";
			if(action.actionType==-1){
				obj += "<input class='checkbox' type='checkbox' id='ckid' name='chkItem' value='"+action.actionCode+"' checked/>"+action.actionName;
			}else{
				obj += "<input class='checkbox' type='checkbox' id='ckid' name='chkItem' value='"+action.actionCode+"'/>"+action.actionName;
			} 
			obj += "</li>";
			action = list[17];
			obj += "<li class='limit_option_li'>";
			if(action.actionType==-1){
				obj += "<input class='checkbox' type='checkbox' id='ckid' name='chkItem' value='"+action.actionCode+"' checked/>"+action.actionName;
			}else{
				obj += "<input class='checkbox' type='checkbox' id='ckid' name='chkItem' value='"+action.actionCode+"'/>"+action.actionName;
			} 
			obj += "</li>";
			action = list[18];
			obj += "<li class='limit_option_li'>";
			if(action.actionType==-1){
				obj += "<input class='checkbox' type='checkbox' id='ckid' name='chkItem' value='"+action.actionCode+"' checked/>"+action.actionName;
			}else{
				obj += "<input class='checkbox' type='checkbox' id='ckid' name='chkItem' value='"+action.actionCode+"'/>"+action.actionName;
			}  
			obj += "</li></ul><div class='clear'></div></div>";
			obj += "<div class='big'><div class='title'>低压管理</div><ul class='limit_option_ul'>";
			var action = list[19];
			obj += "<li class='limit_option_li'>";
			if(action.actionType==-1){
				obj += "<input class='checkbox' type='checkbox' id='ckid' name='chkItem' value='"+action.actionCode+"' checked/>"+action.actionName;
			}else{
				obj += "<input class='checkbox' type='checkbox' id='ckid' name='chkItem' value='"+action.actionCode+"'/>"+action.actionName;
			} 
			obj += "</li>";
			action = list[20];
			obj += "<li class='limit_option_li'>";
			if(action.actionType==-1){
				obj += "<input class='checkbox' type='checkbox' id='ckid' name='chkItem' value='"+action.actionCode+"' checked/>"+action.actionName;
			}else{
				obj += "<input class='checkbox' type='checkbox' id='ckid' name='chkItem' value='"+action.actionCode+"'/>"+action.actionName;
			} 
			obj += "</li>";
			action = list[21];
			obj += "<li class='limit_option_li'>";
			if(action.actionType==-1){
				obj += "<input class='checkbox' type='checkbox' id='ckid' name='chkItem' value='"+action.actionCode+"' checked/>"+action.actionName;
			}else{
				obj += "<input class='checkbox' type='checkbox' id='ckid' name='chkItem' value='"+action.actionCode+"'/>"+action.actionName;
			} 
			obj += "</li>";
			action = list[22];
			obj += "<li class='limit_option_li'>";
			if(action.actionType==-1){
				obj += "<input class='checkbox' type='checkbox' id='ckid' name='chkItem' value='"+action.actionCode+"' checked/>"+action.actionName;
			}else{
				obj += "<input class='checkbox' type='checkbox' id='ckid' name='chkItem' value='"+action.actionCode+"'/>"+action.actionName;
			}  
			obj += "</li></ul><div class='clear'></div></div>";
			obj += "<div class='big'><div class='title'>数据统计</div><ul class='limit_option_ul'>";
			action = list[24];
			obj += "<li class='limit_option_li'>";
			if(action.actionType==-1){
				obj += "<input class='checkbox' type='checkbox' id='ckid' name='chkItem' value='"+action.actionCode+"' checked/>"+action.actionName;
			}else{
				obj += "<input class='checkbox' type='checkbox' id='ckid' name='chkItem' value='"+action.actionCode+"'/>"+action.actionName;
			} 
			obj += "</li>";
			action = list[25];
			obj += "<li class='limit_option_li'>";
			if(action.actionType==-1){
				obj += "<input class='checkbox' type='checkbox' id='ckid' name='chkItem' value='"+action.actionCode+"' checked/>"+action.actionName;
			}else{
				obj += "<input class='checkbox' type='checkbox' id='ckid' name='chkItem' value='"+action.actionCode+"'/>"+action.actionName;
			} 
			obj += "</li>";
			action = list[26];
			obj += "<li class='limit_option_li'>";
			if(action.actionType==-1){
				obj += "<input class='checkbox' type='checkbox' id='ckid' name='chkItem' value='"+action.actionCode+"' checked/>"+action.actionName;
			}else{
				obj += "<input class='checkbox' type='checkbox' id='ckid' name='chkItem' value='"+action.actionCode+"'/>"+action.actionName;
			}  
			obj += "</li></ul><div class='clear'></div></div>";
			obj += "<div class='big'><div class='title'>审核管理</div><ul class='limit_option_ul'>";
			action = list[0];
			obj += "<li class='limit_option_li'>";
			if(action.actionType==-1){
				obj += "<input class='checkbox' type='checkbox' id='ckid' name='chkItem' value='"+action.actionCode+"' checked/>"+action.actionName;
			}else{
				obj += "<input class='checkbox' type='checkbox' id='ckid' name='chkItem' value='"+action.actionCode+"'/>"+action.actionName;
			}  
			obj += "</li></ul><div class='clear'></div></div>";
			obj += "<div class='big'><div class='title'>导入导出管理</div><ul class='limit_option_ul'>";
			action = list[23];
			obj += "<li class='limit_option_li'>";
			if(action.actionType==-1){
				obj += "<input class='checkbox' type='checkbox' id='ckid' name='chkItem' value='"+action.actionCode+"' checked/>"+action.actionName;
			}else{
				obj += "<input class='checkbox' type='checkbox' id='ckid' name='chkItem' value='"+action.actionCode+"'/>"+action.actionName;
			}
			obj += "</li></ul><div class='clear'></div></div>";
			obj += "<div class='big'><div class='title'>单线图查看</div><ul class='limit_option_ul'>";
			action = list[27];
			obj += "<li class='limit_option_li'>";
			if(action.actionType==-1){
				obj += "<input class='checkbox' type='checkbox' id='ckid' name='chkItem' value='"+action.actionCode+"' checked/>"+action.actionName;
			}else{
				obj += "<input class='checkbox' type='checkbox' id='ckid' name='chkItem' value='"+action.actionCode+"'/>"+action.actionName;
			}
			obj += "</li></ul><div class='clear'></div></div></div></div>";
			obj += "<input type='submit' class='submit' value='完 成' onclick='javascrpit:addrole("+roleid+");'/>";
			//alert(obj);
			$(".add_group_box").append(obj);
		}
	});
}

function addrole(rid){
	var rname = $("#cname").val();
	//alert(rname);
	if(rname==""){
		alert("请填写分组名称");
		return false;
	}
	var str="";
	$("input[name='chkItem']:checkbox").each(function(){ 
        if($(this).attr("checked")){
            str += $(this).val()+",";
        }
    }); 
	//alert(str);
	if(str==""){
		alert("请选择权限");
		return false;
	}
	$.ajax({
		url : contextPath+ 'user/role/actionadd',
		data : {
			rolename : rname,
			companyRoleId : rid,
			jurisdiction : str
		},
		type : 'POST',
		success : function(data) {
			alert(data.msgDesc);
			if(data.opResult=='0'){
				$('.add_group_box').hide();
				$('.all_cover').hide();
//				goToPage(1);
				location.href=contextPath+"user/m/rolelist.jsp";
			}
		}
	});
}

function colse(){
	$('.add_group_box').hide(300);
	$('.all_cover').hide(10);
	goToPage(1);
}

function openrole(comanyname,roleid){
	selectComanyAction(comanyname,roleid);
	$('.all_cover').show(10);
    $('.add_group_box').show(100);
}

function delrole(roleid){
	if(confirm('确定删除吗')){
		$.ajax({
			ansyc:false,
			url : contextPath+ 'user/role/actiondel',
			data : {
				companyRoleId : roleid
			},
			type : 'POST',
			success : function(data) {
				if(data.opResult=="0"){
					alert(data.msgDesc);
					//setTimeout("alert("+data.msgDesc+")",500);

					//alert(222);
					goToPage(1);
				}
			}
		});
	}
}
